define(function (require) {
    "use strict";
    var ko = require("knockout");
    var komapping = require("komapping");
    var router = require("plugins/router");
    var layui = require("layui");
    var $ = require("jquery");
    var system = require("durandal/system");
    var shareUrlContent = require("text!../trace/share-url-content.html");

    var acceptPageSize = [10, 30, 60, 90, 150, 300];
    var param = {
        searchDate: ko.observable(""),
        actionId: ko.observable(""),
        codeArrayInclude: ko.observable(""),
        codeArrayExclude: ko.observable(""),
        hostInclude: ko.observable(""),
        hostExclude: ko.observable(""),
        refererInclude: ko.observable(""),
        refererExclude: ko.observable(""),
        protocolInclude: ko.observable(""),
        protocolExclude: ko.observable(""),
        urlInclude: ko.observable(""),
        urlExclude: ko.observable(""),
        typeInclude: ko.observable(""),
        typeExclude: ko.observable(""),
        minSize: ko.observable("-1"),
        curr: 1,
        limit: acceptPageSize[0]
    };

    var currentAction = ko.observable(null);

    var searchUrlGen = function (prefix) {
        return prefix + "?" +
            "searchDate=" + encodeURIComponent(param.searchDate()) +
            "&actionId=" + encodeURIComponent(currentAction() ? currentAction().id : "") +
            "&codeArrayInclude=" + encodeURIComponent(param.codeArrayInclude()) +
            "&codeArrayExclude=" + encodeURIComponent(param.codeArrayExclude()) +
            "&hostInclude=" + encodeURIComponent(param.hostInclude()) +
            "&hostExclude=" + encodeURIComponent(param.hostExclude()) +
            "&refererInclude=" + encodeURIComponent(param.refererInclude()) +
            "&refererExclude=" + encodeURIComponent(param.refererExclude()) +
            "&protocolInclude=" + encodeURIComponent(param.protocolInclude()) +
            "&protocolExclude=" + encodeURIComponent(param.protocolExclude()) +
            "&urlInclude=" + encodeURIComponent(param.urlInclude()) +
            "&urlExclude=" + encodeURIComponent(param.urlExclude()) +
            "&typeInclude=" + encodeURIComponent(param.typeInclude()) +
            "&typeExclude=" + encodeURIComponent(param.typeExclude()) +
            "&minSize=" + encodeURIComponent(param.minSize());
    };

    var images = ko.observableArray([]);
    var totalWidth = ko.observable(0);
    var totalHeight = ko.observable(0);

    var resizeWidthAndHeight = function () {
        totalWidth($(".list-images-content").width());
        totalHeight($(".list-images-content").height());
    };

    $(window).resize(function () {
        resizeWidthAndHeight();
    });

    //var tableIns = null;

    var modelId = ko.observable(null);

    var loadImages = function (page, limit) {
        var url = searchUrlGen("/back/planUrls") +
            "&planId=" + modelId() +
            "&page=" + page +
            "&limit=" + limit;

        $.ajax({
            type: "get",
            url: url,
            dataType: "json"
        }).then(function (response) {
            layui.use(["laypage"], function () {
                var laypage = layui.laypage;
                laypage.render({
                    elem: "page-content",
                    count: response.count,
                    curr: page,
                    limit: limit,
                    limits: [18],
                    layout: ["count", "prev", "page", "next", "limit", "skip"],
                    jump: function (obj, first) {
                        //首次不执行
                        if (!first) {
                            loadImages(obj.curr, obj.limit);
                        }
                    }
                });
                images(response.data);
            });

        }, function (data) {
            layui.use(["layer"], function () {
                var layer = layui.layer;
                layer.alert(data.responseJSON.msg);
            });
        });
    };

    return {
        activate: function (id, queryParam) {
            //重置部分参数防止缓存
            param.actionId("");
            param.searchDate("");

            modelId(id);
            if (!((typeof queryParam === "object") && queryParam)) {
                queryParam = {};
            }
            if ((typeof queryParam.actionId === "string") && queryParam.actionId) {
                param.actionId(queryParam.actionId);
            } else {
                if ((typeof queryParam.searchDate === "string") && queryParam.searchDate) {
                    param.searchDate(queryParam.searchDate);
                }
            }
            if ((typeof queryParam.codeArrayInclude === "string") && queryParam.codeArrayInclude) {
                param.codeArrayInclude(queryParam.codeArrayInclude);
            } else {
                param.codeArrayInclude("");
            }
            if ((typeof queryParam.codeArrayExclude === "string") && queryParam.codeArrayExclude) {
                param.codeArrayExclude(queryParam.codeArrayExclude);
            } else {
                param.codeArrayExclude("");
            }
            if ((typeof queryParam.hostInclude === "string") && queryParam.hostInclude) {
                param.hostInclude(queryParam.hostInclude);
            } else {
                param.hostInclude("");
            }
            if ((typeof queryParam.hostExclude === "string") && queryParam.hostExclude) {
                param.hostExclude(queryParam.hostExclude);
            } else {
                param.hostExclude("");
            }
            if ((typeof queryParam.refererInclude === "string") && queryParam.refererInclude) {
                param.refererInclude(queryParam.refererInclude);
            } else {
                param.refererInclude("");
            }
            if ((typeof queryParam.refererExclude === "string") && queryParam.refererExclude) {
                param.refererExclude(queryParam.refererExclude);
            } else {
                param.refererExclude("");
            }
            if ((typeof queryParam.protocolInclude === "string") && queryParam.protocolInclude) {
                param.protocolInclude(queryParam.protocolInclude);
            } else {
                param.protocolInclude("");
            }
            if ((typeof queryParam.protocolExclude === "string") && queryParam.protocolExclude) {
                param.protocolExclude(queryParam.protocolExclude);
            } else {
                param.protocolExclude("");
            }
            if ((typeof queryParam.urlInclude === "string") && queryParam.urlInclude) {
                param.urlInclude(queryParam.urlInclude);
            } else {
                param.urlInclude("");
            }
            if ((typeof queryParam.urlExclude === "string") && queryParam.urlExclude) {
                param.urlExclude(queryParam.urlExclude);
            } else {
                param.urlExclude("");
            }
            if ((typeof queryParam.typeInclude === "string") && queryParam.typeInclude) {
                param.typeInclude(queryParam.typeInclude);
            } else {
                param.typeInclude("");
            }
            if ((typeof queryParam.typeExclude === "string") && queryParam.typeExclude) {
                param.typeExclude(queryParam.typeExclude);
            } else {
                param.typeExclude("");
            }
            if ((typeof queryParam.minSize === "string") && queryParam.minSize) {
                param.minSize(queryParam.minSize);
            } else {
                param.minSize("");
            }
            var promise = null;
            if (param.actionId()) {
                promise = $.ajax({
                    type: "get",
                    url: "/back/actionById/" + param.actionId(),
                    dataType: "json"
                }).then(function (response) {
                    currentAction(response.data);
                }, function (data) {
                    layui.use(["layer"], function () {
                        var layer = layui.layer;
                        layer.alert(data.responseJSON.msg);
                    });
                });
            } else {
                promise = $.ajax({
                    type: "get",
                    url: "/back/actionByTime",
                    dataType: "json",
                    data: {time: encodeURIComponent(param.searchDate()), planId: modelId()}
                }).then(function (response) {
                    currentAction(response.data);
                }, function (data) {
                    layui.use(["layer"], function () {
                        var layer = layui.layer;
                        layer.alert(data.responseJSON.msg);
                    });
                });
            }
            return promise;
        },
        param: param,
        currentAction: currentAction,
        modelId: modelId,
        images: images,
        totalWidth: totalWidth,
        totalHeight: totalHeight,
        compositionComplete: function () {
            layui.use(["laytpl", "table", "element", "layer", "laydate", "form"], function () {
                var laytpl = layui.laytpl;
                //var table = layui.table;
                var element = layui.element;
                var layer = layui.layer;
                var laydate = layui.laydate;
                var form = layui.form;

                resizeWidthAndHeight();
                loadImages(1, 18);

                form.on("submit(search-submit)", function (data) {
                    loadImages(1, 18);
                    return false;
                });

                $("#share-url").click(function (data) {
                    var url = searchUrlGen("http://" + window.location.host + "/kunlunAssets/index.html#plan/review/param/" + modelId());
                    layer.open({
                        content: shareUrlContent,
                        shadeClose: true,
                        btn: [],
                        title: "请复制",
                        success: function (layero, index) {
                            var inputDom = $(layero.selector).find(".share-url-input");
                            inputDom.val(url);
                            inputDom.select();
                        }
                    });
                    return false;
                });

                $("#share-params-only").click(function () {
                    var paramObj = komapping.toJS(param);
                    delete paramObj.searchDate;
                    delete paramObj.actionId;
                    delete paramObj.curr;
                    delete paramObj.limit;
                    layer.open({
                        content: shareUrlContent,
                        shadeClose: true,
                        btn: [],
                        title: "请复制",
                        success: function (layero, index) {
                            var inputDom = $(layero.selector).find(".share-url-input");
                            inputDom.val(JSON.stringify(paramObj));
                            inputDom.select();
                        }
                    });
                    return false;
                });

                $("#back-to-param").click(function (data) {
                    var url = searchUrlGen("http://" + window.location.host + "/kunlunAssets/index.html#plan/review/param/" + modelId());
                    router.navigate(url);
                    return false;
                });

                $("#download-link").click(function (data) {
                    var url = searchUrlGen("http://" + window.location.host + "/back/excel/export/" + modelId());
                    var iframe = $("<iframe style='display: none;'></iframe>");
                    iframe.attr("src", url);
                    $("body").append(iframe);
                    return false;
                });
            });
        }
    };
});